package com.anlogic.sdk.utils.options;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public enum UartDevice {
	UART0("UART0", "UART0"), UART1("UART1", "UART1");

	private String id;
	
	private String name;

	private static Map<String, UartDevice> idMap = new LinkedHashMap<>();

	private static Map<String, UartDevice> nameMap = new LinkedHashMap<>();

	static {
		for (UartDevice value : UartDevice.values()) {
			idMap.put(value.getId(), value);
			nameMap.put(value.getName(), value);
		}
	}

	UartDevice(String id, String name) {
		this.id = id;
		this.name = name;
	}

	public String getId() {
		return this.id;
	}

	public String getName() {
		return this.name;
	}

	public static String[] getIds() {
		return idMap.keySet().toArray(new String[0]);
	}

	public static String[] getNames() {
		return nameMap.keySet().toArray(new String[0]);
	}

	public static UartDevice getTypeById(String id) {
		return idMap.get(id);
	}

	public static UartDevice getTypeByName(String name) {
		return nameMap.get(name);
	}
	
	public static int getIndex(String name) {
		 Pattern pattern = Pattern.compile("(\\d+)");
	     Matcher matcher = pattern.matcher(name);
	     if (matcher.find()) {
	    	 String ret = matcher.group(1);
	    	 return Integer.parseInt(ret);
	     }
	     return -1;
	}
}
